package com.wlb.es.controller;

import com.wlb.es.entity.User;
import com.wlb.es.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("/create/{indexName}")
    public String createIndex(@PathVariable("indexName")String indexName){
        userService.createIndex(indexName);
        return "创建成功！";
    }

    /**
     * 添加用户
     * @param user
     * @param indexName
     * @return
     */
    @PostMapping("/add/{indexName}")
    public String add(@RequestBody User user,@PathVariable("indexName") String indexName){
        userService.add(user,indexName);
        return "添加成功";
    }

    /**
     * 批量添加用户
     * @param users
     * @param indexName
     * @return
     */
    @PostMapping("/adds/{indexName}")
    public String addUsers(@RequestBody List<User> users,@PathVariable("indexName") String indexName){
        userService.addUsers(users,indexName);
        return "添加成功";
    }

    /**
     * 根据id查询一个user
     * @param id
     * @param indexName
     * @return
     */
    @GetMapping("/find/{indexName}/{id}")
    public User findById(@PathVariable("id") String id,@PathVariable("indexName") String indexName){
        return userService.findById(id,indexName);
    }

    /**
     * 查询所有用户
     * @param indexName
     * @return
     */
    @GetMapping("/findAll/{indexName}")
    public List<User> findAll(@PathVariable("indexName") String indexName){
        return userService.findAll(indexName);
    }

    /**
     * 分页查询
     * @param indexName
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/page/{indexName}")
    public List<User> pageQuery(@PathVariable("indexName") String indexName, Integer page, Integer size){
        if(page==null){
            page=0;
        }
        if(size==null){
            size=10;
        }
        return userService.pageQuery(indexName,page,size);
    }

    /**
     * 根据id删除用户
     * @param id
     * @param indexName
     * @return
     */
    @PostMapping("/delete/{indexName}/{id}")
    public String deleteById(@PathVariable("id") String id,@PathVariable("indexName") String indexName){
        userService.deleteById(id,indexName);
        return "删除成功";
    }

    /**
     * 批量删除用户
     * @param ids
     * @param indexName
     * @return
     */
    @PostMapping("/deletes/{indexName}")
    public String deleteByIds(@RequestBody List<String> ids,@PathVariable("indexName") String indexName){
        userService.deleteByIds(ids,indexName);
        return "删除成功";
    }

    /**
     * 修改用户
     * @param user
     * @param indexName
     * @return
     */
    @PostMapping("/update/{indexName}")
    public String updateById(@RequestBody User user,@PathVariable("indexName") String indexName){
        userService.updateById(user,indexName);
        return "更新成功";
    }

}
